package com.my.app.controller;

import com.my.sys.model.SysResource;
import com.my.sys.model.SysUser;
import com.my.sys.service.SysResourceService;
import com.my.sys.service.SysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class AppController {

    private final Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourceService sysResourceService;

    @RequestMapping(value = "live-chat", method = RequestMethod.GET)
    public ModelAndView liveChat(Model model, @RequestParam String demo) {
        logger.info("live-chat arg demo: {}", demo);
        model.addAttribute("curl", "");
        return new ModelAndView("inc/live-chat");
    }

    @RequestMapping(value = "header", method = RequestMethod.GET)
    public ModelAndView header(Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            String username = (String) subject.getPrincipal();
            logger.info("Loading header for user: {}", username);
            SysUser user = sysUserService.findByUsername(username);
            model.addAttribute("user", user);
        }
        return new ModelAndView("inc/header");
    }

    @RequestMapping(value = "sidebar", method = RequestMethod.GET)
    public ModelAndView sidebar(Model model, @RequestParam String[] pos) {
        logger.info("sidebar pos: {}", pos);
        if (StringUtils.isEmpty(pos)) {
            pos = new String[]{"dashboard"};
        }
        model.addAttribute("pos", pos);

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            String username = (String) subject.getPrincipal();
            logger.info("Loading header for user: {}", username);
            SysUser user = sysUserService.findByUsername(username);
            //List<SysResource> menus = sysResourceService.findByRoles(user.getRoles());
            List<SysResource> menus = sysResourceService.findNav();
            if (!menus.isEmpty()) {
                logger.info("menus size: {}", menus.size());
                model.addAttribute("menus", menus);
            }
        }
        return new ModelAndView("inc/sidebar");
    }

    @RequestMapping(value = "slide-panel", method = RequestMethod.GET)
    public ModelAndView slidePanel(Model model, @RequestParam String demo) {
        logger.info("slide-panel arg demo: {}", demo);
        model.addAttribute("curl", "");
        return new ModelAndView("inc/slide-panel");
    }

    @RequestMapping(value = "profile", method = RequestMethod.GET)
    public ModelAndView profile(Model model) {
        logger.info("profile");
        model.addAttribute("curl", "");
        return new ModelAndView("profile");
    }

    @RequestMapping(value = "contact", method = RequestMethod.GET)
    public ModelAndView contact(Model model) {
        logger.info("contact");
        model.addAttribute("curl", "");
        return new ModelAndView("contact");
    }


    @RequestMapping(value = "404", method = RequestMethod.GET)
    public ModelAndView error404(Model model) {
        logger.info("404: page not found");
        model.addAttribute("curl", "");
        return new ModelAndView("404");
    }
}
